package com.secret.view;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.TextAlignment;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class VerifyPasswordDialog extends Stage {

    private PasswordField passwordTf;

    public VerifyPasswordDialog() {
        this.setTitle("请输入密码");
        this.initModality(Modality.APPLICATION_MODAL);
        this.setWidth(400);
        this.setHeight(250);

        VBox vBox = new VBox();
        vBox.setPadding(new Insets(20 , 50 , 20 , 50));
        vBox.setSpacing(16);
        Label label = new Label("请输入密码");
        label.setTextAlignment(TextAlignment.CENTER);
        vBox.setAlignment(Pos.TOP_CENTER);
        label.setFont(Font.font(16));
        vBox.getChildren().add(label);
        Scene scene = new Scene(vBox);
        this.setScene(scene);
        scene.setOnKeyReleased(event -> {
            // 按esc关闭窗口
            if(event.getCode().equals(javafx.scene.input.KeyCode.ESCAPE)){
                this.close();
            }
        });


        this.passwordTf = new PasswordField();
        passwordTf.setOnKeyReleased(event -> {
            if(event.getCode().equals(javafx.scene.input.KeyCode.ENTER)){
                this.onOk();
            }
        });
        passwordTf.setFont(Font.font(16));
        vBox.getChildren().add(passwordTf);

        Button okBtn = new Button("确定");
        VBox.setMargin(okBtn , new Insets(30 , 0 , 0 , 0));
        okBtn.setFont(Font.font(16));
        okBtn.prefWidthProperty().bind(vBox.widthProperty());
        okBtn.setOnAction(event -> {
            this.onOk();
        });
        vBox.getChildren().add(okBtn);
    }

    private void onOk(){
        String password = getPassword();
        if(password == null || password.trim().isBlank()){
            return;
        }
        this.close();
    }

    public String getPassword(){
        return passwordTf.getText().trim();
    }
}
